package com.bruce.starts.demo.poi.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.bruce.starts.demo.poi.dto.ProdGrayClothVatReportDTO;
import com.bruce.starts.demo.poi.dto.ProdGrayClothVatStockBillVO;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class EasyPoiExportDemo {
    public static void main(String[] args) throws IOException {
        export();
    }
    public static void export() throws IOException {
        // 获取模板
        TemplateExportParams params = new TemplateExportParams(
                "F:\\致景科技\\03_开发\\03_开发任务\\03_进销存系统\\三阶段\\完缸结算表.xlsx");
        Map<String, Object> map = new HashMap<String, Object>();
        ProdGrayClothVatReportDTO dto = createDTO(4);
        map = dtoToMap(dto);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("F:\\致景科技\\03_开发\\03_开发任务\\03_进销存系统\\三阶段");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("F:\\致景科技\\03_开发\\03_开发任务\\03_进销存系统\\三阶段\\专test.xlsx");
        workbook.write(fos);
    }

    public static ProdGrayClothVatReportDTO createDTO(int num){
        ProdGrayClothVatReportDTO dto = new ProdGrayClothVatReportDTO();
        dto.setCreateTime(new Date());
        dto.setCustomerName("罗团");
        dto.setDyeingFactoryName("测试厂1");
        dto.setVatNum("测试缸号");
        dto.setColor("红色");
        dto.setHeadShare(100);
        dto.setTotalMeters(new BigDecimal(100));
        dto.setTotalAxisNum(2);
        dto.setVarietyStandard("规则11111");
        dto.setVarietyName("测试品种");
        dto.setFactoryId("测试工厂");
        List<ProdGrayClothVatStockBillVO> voList = new ArrayList<>();
        for(int i=0;i<num;i++){
            ProdGrayClothVatStockBillVO vo = new ProdGrayClothVatStockBillVO();
            vo.setOrderDate("2021-08-04");
            vo.setVatNum("xxxx233");
            vo.setVarietyName("测试品种");
            vo.setReelNum(new BigDecimal(ThreadLocalRandom.current().nextInt(20)));
            vo.setMeterNum(new BigDecimal(ThreadLocalRandom.current().nextInt(120)));
            vo.setWeftDensity(new BigDecimal(ThreadLocalRandom.current().nextInt(100)));
            voList.add(vo);
        }
        dto.setVoList(voList);
        return dto;
    }

    public static Map<String,Object> dtoToMap(ProdGrayClothVatReportDTO dto){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("customerName",dto.getCustomerName());
        map.put("createDate", DateTimeFormatter.ofPattern("yyyy-MM-hh").format(LocalDateTime.now()));
        map.put("dyeingFactoryName",dto.getDyeingFactoryName());
        map.put("vatNum",dto.getVatNum());
        map.put("color",dto.getColor());
        map.put("headShare",dto.getHeadShare());
        map.put("totalMeters",dto.getTotalMeters());
        map.put("totalAxisNum",dto.getTotalAxisNum());
        map.put("factory",dto.getFactoryId());
        map.put("varietyName",dto.getVarietyName());
        map.put("varietyStandard",dto.getVarietyStandard());
        List<ProdGrayClothVatStockBillVO> voList = dto.getVoList();
        List<Map<String, Object>> listMap = new ArrayList<>();
        voList.forEach(vo->{
             Map<String, Object> voMap = new HashMap<>();
             voMap.put("date",vo.getOrderDate());
             voMap.put("varietyNum",vo.getVatNum());
             voMap.put("varietyName",vo.getVarietyName());
             voMap.put("roll",vo.getReelNum());
             voMap.put("meter",vo.getMeterNum());
             voMap.put("weftDensity",vo.getWeftDensity());
            listMap.add(voMap);
        });
        map.put("mapList",listMap);
        return map;
    }
}
